hold a lock on the same resource at the same time. The security of the lock is broken. The Redis authors have designed the Redlock algorithm for possible loss of lock security during Redis master-slave replication.
The client that runs the Redlock algorithm performs the following steps in turn to complete the operation to acquire the lock : Gets the current time (number of milliseconds). The performs
of the lock was broken. To solve this problem, Antirez designed the Redlock algorithm.
"Other questions"
The effective time of the lock that appears in the previous algorithm (lock validity times) is set to how appropriate. If the setting is too short, locks are likely to expire before the client completes access to the shared resource, thereby losing protection, and if the setting is too long, if a client holding the lock fails to release the lock
of code, the Isopenexpirationrenewal property to False, and the thread polling to stop flushing the expiration time.
public void unlock() {
//...
isOpenExpirationRenewal = false;
}
Full code for version 5: GitHub address3.7 Testing
The test code is as follows
public void testLockCase5() {
//定义线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor(0, 10,
1, TimeUnit.SECONDS,
code to the Jedis.eval () method and assign the parameter keys[1] to
Lockkey, Argv[1] is assigned a value of
RequestID。 The eval () method is to deliver the LUA code to the Redis server for execution.
Five Redlock
Redlock is an official Redis distributed lock principle. Official document Description: "There are a lot of tripartite libraries and articles describing how to implement a distributed lock
prefix and unique ID into one channel name , and then wait the same as the previous example. Examples are as follows:Import randomsingle_cast_script= "" "Local channels = Redis.call (' pubsub ', ' channels ', argv[1] ... *‘); If #channels = = 0 then return 0; End Local index= Math.mod (Math.floor (Tonumber (argv[2)), #channels) + 1; Return Redis.call (' Publish ', Channels[index], argv[3]); Def wait_single (channel, myID): Return Wait (Channel + myID) def signal_single (channel, data): rand_nu
lock based on Redis, the Redlock algorithm is presented, and the implementation cases of many languages are listed, here is a brief introduction.
The Redlock algorithm looks at three elements that meet distributed locks:
Security: Guarantees mutual exclusion, at most at any time only one client can hold locks
Deadlock free: Even if the client that is currently holding the lock crashes or is separated from
', $timeout, ' NX ');//ex sec
if ($isLock) {
if Redis:: Get ($key) = = $value) { //Prevent early expiration, mistakenly delete other requests created by the lock
//Execute internal code
redis::d El ($key);
continue;//execution successfully deletes the key and jumps out of the loop
}
else {
usleep (5000);//sleep, reduce the lock frequency, ease the redis pressure,
A distributed lock is a very useful primitive in many environments, and it is the only way to share resources between mutually exclusive operations of a process. There are a number of development libraries and blogs that describe how to implement DLM (distributed Lock Manager) using Redis, but each development library uses a different approach, and many libraries are implemented in simple, low reliable ways compared to more complex designs and implementations.
This article attempts to provide a
problem is due to the use of master-slave replication, the solution is not to use the master-slave replication, using the Redlock algorithm, here refers to a section of the online description of the Redlock algorithm.In a REDIS distributed environment, assuming that there are 5 Redis master, these nodes are completely independent of each other, there is no master-slave replication or other cluster coordina
, but the master-slave replication mechanism is asynchronous, there will be data synchronization problems, may lead to multiple machines multiple threads to obtain the same lock; the solution is to use the master-slave replication caused by the problem, the solution is not the master-slave replication, using the Redlock algorithm;
Redlock is described as follows: In a Redis distributed environment, ass
,redissonclient redisson2, redissonclient redisson3) {
Rlock lock1 = Redisson1.getlock ("Lock1");
Rlock Lock2 = Redisson2.getlock ("Lock2");
Rlock Lock3 = Redisson3.getlock ("Lock3");
Redissonmultilock lock = new Redissonmultilock (Lock1, Lock2, LOCK3);
try {
//Lock at the same time: Lock1 Lock2 Lock3, all the locks were successfully locked.
Lock.lock ();
//Try to lock, wait up to 100 seconds, automatically unlock after 10 seconds of lock
Boolean res = Lock.trylock (+, tim
counter based on Redis?
The implementation mechanism of Redis distributed lock and Redlock is briefly introduced.
What is a consistent hash? Is there a corresponding module in Python?
How to efficiently find all keys in Redis that start with Oldboy?
Part Four front end, frame and others (155 questions)
Talk about your understanding of the HTTP protocol.
Talk about your understanding of the WebSocket agreement.
What is a magic str
) | Seventh: Highly Available Distributed Configuration Center (Spring Cloud Config) The simplest springcloud tutorials in history | Eighth: The simplest springcloud tutorials in the history of the message bus (Spring Cloud buses) | Nineth: Service link Tracking (Spring Cloud Sleuth) The simplest Springcloud tutorials in history | Tenth: The most simple springcloud tutorials in the history of highly available service registries | 11th: Docker deployment of the simplest springcloud tutorials in t
(Hystrix Turbine)] (http://blog.csdn.net/forezp/article/details/70233227)* [The simplest springcloud tutorial in history | 14th: Service Registration (consul)] (http://blog.csdn.net/forezp/article/details/70245644)* Not finished ...* There are many other articles ...# # # of foreign articles:* [How to use Mongodb+springboot to implement distributed ID?] (http://blog.csdn.net/forezp/article/details/69056017)* [How to implement distributed locks in Springcloud distributed systems.] ] (http://blog
?34 lists the expiration policies supported by Redis.MySQL has 2000w data, Redis only 20w of data, how to ensure that Redis is hot data?36 Write code, the Redis-based list implements the FIFO, LIFO, priority queue.37 How do I implement Message Queuing based on Redis?38 How can I implement publishing and subscriptions based on Redis? And the difference between a publish subscription and a message queue?39 What is Codis and function?40 What is twemproxy and function?41 write code to implement Redi
default. If the lock is not released, the lock will be automatically eliminated after 10 s.
The value set when the lock is obtained is also saved. When the lock is released, the system first checks whether the saved value is the same as the current lock value. If not, it indicates that the lock is automatically released after expiration and is obtained by other processes. Therefore, the lock value must be unique to avoid releasing the lock obtained by other programs.
Use lock:
def a_to_b():
Reference link for Redis distributed locks: http://redis.io/topics/distlock.In our project, before pondering using: Servicestack.redis, found that Servicestack.redis charges, operating more than 6,000 times per hour error : "The Free-quota limit on ' 6000 Redis requests per hour ' has been reached ... "So look at other Redis clients:Stackexchange.redis seemingly does not support distributed locks , or I do not know?Redlock.net and Redlock-cs are bad f
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.